import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Spxt20Package {

    //工程路径 D:\IdeaProjects\zgfspNew
//    private static String projectPath = "E:\\IdeaProjects\\zgfsp_bbtj"; // --------(需修改)
//    private static String projectPath = "E:\\IdeaProjects\\spjkpt2022"; // --------(需修改)
//    private static String projectPath = "E:\\IdeaProjects\\spjkpt2022_bbtj"; // --------(需修改)
//    private static String projectPath = "E:\\IdeaProjects\\swhjkpt_zgy"; // --------(需修改)
//    private static String projectPath = "E:\\IdeaProjects\\zgfsp"; // --------(需修改)
    private static String projectPath = "E:\\IdeaProjects\\court2015"; // --------(需修改)
//    private static String projectPath = "E:\\IdeaProjects\\court2022_bbtj"; // --------(需修改)
//    private static String projectPath = "E:\\IdeaProjects\\scks"; // --------(需修改)

    //工程编译java文件路径(二选一)
    //maven项目class文件路劲
    private static String maven_classPath = projectPath + "\\target\\classes\\"; // --------(maven项目修改此处,非maven项目无需修改)
    //普通web项目class文件路劲
    private static String classPath = projectPath + "\\WebContent\\WEB-INF\\classes\\";

    // 是否maven项目
    private static boolean isMaven = true; // --------(需修改)

    //日志文件
    private static String logfilePath = "C:\\Users\\DELL\\Desktop\\mjw\\临时补丁\\changeLog_20spxt.txt";

    //输出补丁文件地址
//    private static String aimpath1 = "C:/Users/DELL/Desktop/mjw/临时补丁/spjkpt2022/"; // --------(需修改)
//    private static String aimpath1 = "C:/Users/DELL/Desktop/mjw/临时补丁/swhjkpt_zgy/"; // --------(需修改)
//    private static String aimpath1 = "C:/Users/DELL/Desktop/mjw/临时补丁/zgfsp/"; // --------(需修改)
    private static String aimpath1 = "C:/Users/DELL/Desktop/mjw/临时补丁/20SP/"; // --------(需修改)
//    private static String aimpath1 = "C:/Users/DELL/Desktop/mjw/临时补丁/scks/"; // --------(需修改)


    public static void main(String[] args){
        File file = new File(logfilePath);

        // 按行读取字符串
        try{
            RandomAccessFile fileR = new RandomAccessFile(file,"r");
            String str = null;
            boolean isReadFilePath = false;
            while ((str = fileR.readLine())!= null) {
//                System.out.println(str);
                if ("Changed paths:".equals(str)){
                    isReadFilePath = true;
                    continue;
                }
                if("".equals(str)){
                    isReadFilePath = false;
                    continue;
                }
                /*if(!isReadFilePath){
                    continue;
                }*/
                if (str.endsWith(".java")){
                    String _classPath = str.replace(".java",".class");
                    //class文件地址
                    String oripath = "";
                    if(isMaven){
                        _classPath = _classPath.replace("src/main/java", "");
                        oripath = maven_classPath + _classPath;
                    }else{
                        _classPath = _classPath.replace("src", "");
                        oripath = classPath + _classPath;
                    }
                    
                    String _copyPath = aimpath1 + "/WEB-INF/classes/" + _classPath;
                    FileUtils.copyFile(new File(oripath), new File(_copyPath));
                    copynm(oripath, new File(_copyPath).getParent() + "/");
                }else { //if (str.endsWith(".xml") || str.endsWith(".jsp") || str.endsWith(".js") || str.endsWith(".png") || str.endsWith(".tld") || str.endsWith(".docx") || str.endsWith(".css")){
                    String resourcePath = str;
                    if(isMaven){
                        resourcePath = resourcePath.replace("src/main/webapp", "");
                    }else{
                        resourcePath = resourcePath.replace("WebContent","");
                    }
//                    String oripath = projectPath + "/src/main/webapp" + resourcePath;
                    String oripath = projectPath + "/" + str;
                    String _copyPath = aimpath1 + resourcePath;
                    FileUtils.copyFile(new File(oripath),new File(_copyPath));
                }
                //枚举classs、pom、jar包
                System.out.println(str);
            }
            fileR.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Description 复制内部类对应的class文件
     * @Param oripath 文件本地地址
     * @Param aimpath 文件复制地址
     * @Return void
     * @Author zhoushuo
     * @Date 2019/9/23 1:01
     * @Version 1.0
     */
    public static void copynm(String oripath, String aimpath) throws IOException {
        String fname = FilenameUtils.getBaseName(oripath);
        System.out.println("----"+fname);
        File floder = new File(oripath).getParentFile();
        if(floder.listFiles()!=null) {
            for (File f : floder.listFiles()) {
                if (f.getName().indexOf(fname) > -1 && f.getName().indexOf("$") > -1) {
                    FileUtils.copyFile(f, new File(aimpath + f.getName()));
                }
            }
        }

    }

}

